package cn.itguy.mobileguard.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import cn.itguy.mobileguard.ActivityJumper;
import cn.itguy.mobileguard.CommonUtils;
import cn.itguy.mobileguard.R;
import cn.itguy.mobileguard.activity.MainActivity.ViewPagerVisibilityListener;
import cn.itguy.mobileguard.service.IGuardService;

public class MoreFragment extends ListFragment implements
		ViewPagerVisibilityListener {

	private boolean running;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_more, container,
				false);
		((TextView) rootView.findViewById(R.id.tv_title))
				.setText(R.string.titleText_more);

		setListAdapter(new MoreListViewAdapter(getActivity()));

		return rootView;
	}

	@Override
	public void onVisibilityChanged(boolean visible) {
		if (visible) {
			running = CommonUtils.isServiceRunning(getActivity(),
					IGuardService.class.getName());
			((MoreListViewAdapter) getListAdapter()).notifyDataSetChanged();
		}
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		switch (position) {
		case 0:
			if (running) {
				Toast.makeText(
						getActivity(),
						R.string.tipText_can_not_do_settings_when_service_running,
						Toast.LENGTH_SHORT).show();
			} else {
				ActivityJumper.toConfigActivity(getActivity());
			}
			break;
		case 1:
			ActivityJumper.toHelpActivity(getActivity());
			break;
		case 2:
			break;
		// case 3:
		// ActivityJumper.toContactActivity(getActivity());
		// break;
		 case 3:
		 ActivityJumper.toAboutActivity(getActivity());
		 break;
		}
	}

	private final class MoreListViewAdapter extends BaseAdapter {

		private LayoutInflater inflater;

		private String[] more_items;

		public MoreListViewAdapter(Context context) {
			this.inflater = LayoutInflater.from(context);
			more_items = context.getResources().getStringArray(
					R.array.more_items);
		}

		@Override
		public int getCount() {
			return more_items.length;
		}

		@Override
		public Object getItem(int position) {
			return more_items[position];
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = inflater.inflate(R.layout.more_list_item, null);
				holder.itemTextView = (TextView) convertView
						.findViewById(R.id.itemTextView);
				holder.itemImageView = (ImageView) convertView
						.findViewById(R.id.itemImageView);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.itemTextView.setText(more_items[position]);
			if (running && position == 0) {
				holder.itemImageView.setImageResource(R.drawable.ic_lock);
			} else {
				holder.itemImageView.setImageResource(R.drawable.ic_arrow);
			}

			if (position == 0) {
				convertView
						.setBackgroundResource(R.drawable.selector_small_corner_listview_top_item);
			} else if (position == more_items.length - 1) {
				convertView
						.setBackgroundResource(R.drawable.selector_small_corner_listview_bottom_item);
			} else {
				convertView
						.setBackgroundResource(R.drawable.selector_small_corner_listview_common_item);
			}

			return convertView;
		}

	}

	private static class ViewHolder {
		TextView itemTextView;
		ImageView itemImageView;
	}

}
